//
//  YTFWelcomeViewController.swift
//  sinaWeibo
//
//  Created by pro on 16/2/29.
//  Copyright © 2016年 pro. All rights reserved.
//

import UIKit
import SDWebImage


class YTFWelcomeViewController: UIViewController {

    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    //视图出现后再调用动画
    override func viewDidAppear(animated: Bool) {
        super.viewWillAppear(animated)
        startAnimation()
    }
    
    //开启动画
    private func startAnimation(){
        
        //更新约束
        self.photoImageView.snp_updateConstraints { (make) -> Void in
            
            make.bottom.equalTo(self.view.snp_bottom).offset(-UIScreen.mainScreen().bounds.height+200)
            
            //其他约束不要修改，直接复制
            make.centerX.equalTo(self.view.snp_centerX)
            
            //设置一个宽高
            make.size.equalTo(CGSizeMake(170/2, 170/2))
        }
        
        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            //完成约束动画的时候，我们需要调用下面这个方法，切记！！！！
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
//                print("完成")
                //切换视图
                NSNotificationCenter.defaultCenter().postNotificationName("changeRootVC", object: nil)
        }
        
        
    }
    
    //添加控件和约束
    private func setupUI(){
        
        view.addSubview(photoImageView)
        view.addSubview(nameLabel)
        
        //设置约束
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-150)
            make.centerX.equalTo(view.snp_centerX)
            
            //设置宽高
            make.size.equalTo(CGSizeMake(170/2, 170/2))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(photoImageView.snp_bottom).offset(10)
            make.centerX.equalTo(view.snp_centerX)
        }
        
        //头像修改
        let url = NSURL(string: YTFUserViewModel.shareInstance.model?.profile_image_url ?? "")
        if let Ourl = url{
            photoImageView.sd_setImageWithURL(Ourl, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
    }
    
    
    //懒加载
    //背景图片
    private lazy var bgImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "ad_background")
        
        return img
        
    }()
    
    //头像
    private lazy var photoImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        img.layer.cornerRadius = 170 / 4
        
        img.layer.masksToBounds = true
        
        img.layer.borderColor = UIColor.grayColor().CGColor
        
        img.layer.borderWidth = 2
        
        return img
    }()
    
    
    //欢迎标语
    private lazy var nameLabel: UILabel = {
        
        let label = UILabel()
        
        label.textAlignment = .Center
        
        label.font = UIFont.systemFontOfSize(15)
        
        label.textColor = UIColor.darkGrayColor()
        
        label.text = "\(YTFUserViewModel.shareInstance.model?.screen_name ?? "")欢迎归来"
        return label
        
    }()
   
}
